package com.gtvtv.sparkdesksdk;

import com.gtvtv.sparkdesksdk.client.AgentClient;
import com.gtvtv.sparkdesksdk.model.request.agent.AgentRequest;
import com.gtvtv.sparkdesksdk.model.request.agent.AgentRequestBody;
import com.gtvtv.sparkdesksdk.model.request.agent.AgentRequestHeader;
import com.gtvtv.sparkdesksdk.model.response.agent.AgentResponse;
import com.gtvtv.sparkdesksdk.model.response.agent.Choice;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

// 使用示例
public class AgentDemo {
    public static void main(String[] args) {
        AgentClient client = new AgentClient("2ae2e528ef23445e1945caf2b728ab86", 
                "MmRmMjExOTQzODRiMzA3MTVlZDgxYWU1", "https://xingchen-api.xf-yun.com/workflow/v1/chat/completions");

        // 准备请求
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("AGENT_USER_INPUT", "大便超人");

        AgentRequestBody body = new AgentRequestBody("7345631608903725058", parameters, false);
        AgentRequestHeader header = new AgentRequestHeader(client.apiKey,client.apiSecret);
        AgentRequest request = new AgentRequest(header, body);

        try {
            // 发送普通请求
            AgentResponse response = client.sendRequest(request);

            if (response.isSuccess()) {
                System.out.println("请求成功!");
                System.out.println("工作流ID: " + response.getId());

                for (Choice choice : response.getChoices()) {
                    System.out.println("回复内容: " + choice.getDelta().getContent());
                }

                if (response.getUsage() != null) {
                    System.out.println("Token使用: " + response.getUsage().getTotalTokens());
                }
            } else {
                System.err.println("请求失败: " + response.getCode() + " - " + response.getMessage());
            }

            // 流式请求示例
            body.setStream(true);
            client.processStreamingResponse(body, new AgentClient.StreamCallback() {
                @Override
                public void onData(AgentResponse response) {
                    if (response.getChoices() != null && !response.getChoices().isEmpty()) {
                        String content = response.getChoices().get(0).getDelta().getContent();
                        if (content != null) {
                            System.out.print(content);
                        }
                    }
                }

                @Override
                public void onError(IOException e) {
                    System.err.println("流式请求错误: " + e.getMessage());
                }

                @Override
                public void onComplete() {
                    System.out.println("\n\n流式请求完成");
                }
            });

        } catch (IOException e) {
            System.err.println("请求异常: " + e.getMessage());
        }
    }
}